#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5005,mod = 1e9+7;
int n,k,w[N],a[N],dp[N][N],res[N];
vector<int> g[N];
namespace sub1{
	int f[N],dep[N];
	void dfs(int u,int fa)
	{
		f[u] = fa,dep[u] = dep[fa]+1;
		for(auto v:g[u])
		{
			if(v==fa) continue;
			dfs(v,u);
		}
	}
	pair<int,int> get(int x,int y)
	{
		if(x==0) return {y,a[y]};
		if(y==0) return {x,a[x]};
		int res = 0;
		if(dep[x]<dep[y]) swap(x,y);
		while(dep[x]>dep[y]) res|=a[x],x = f[x];
		while(x!=y) res|=a[x]|a[y],x = f[x],y = f[y];
		return {x,res|a[x]}; 
	}
	inline void main()
	{
		dfs(1,0);
		int ans = 0;
		for(int z = 1;z<(1<<n);z++)
		{
			int l = 0,now = 0,cnt = 0;
			for(int i = 1;i<=n;i++)
				if((z>>i-1)&1)
				{
					cnt++;
					auto _ = get(l,i);
					l = _.first,now|=_.second; 
				}
			(ans+=res[n-cnt]*w[__builtin_popcount(now)])%=mod; 
		}
		cout<<ans;
	}
}
namespace sub2{
	int c[N][N];
	inline void main()
	{
		c[0][0] = 1;
		for(int i = 1;i<=n;i++)
		{
			c[i][0] = 1;
			for(int j = 1;j<=i;j++)
				c[i][j] = (c[i-1][j-1]+c[i-1][j])%mod;
		}
		int ans = 0;
		for(int i = 1;i<=n;i++)
			(ans+=w[1]*c[n][i]%mod*res[n-i])%=mod;
		cout<<ans;
	}
}
signed main()
{
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	dp[0][0] = res[0] = 1;
	for(int i = 1;i<=5000;i++)
		for(int j = 1;j<=i;j++)
			dp[i][j] = (dp[i-1][j-1]+dp[i-1][j]*j)%mod,(res[i]+=dp[i][j])%=mod;
	cin>>n>>k;
	for(int i = 1;i<=k;i++)
		cin>>w[i];
	for(int i = 1,len,x;i<=n;i++)
	{
		cin>>len;
		while(len--) cin>>x,a[i]|=(1<<x-1);
	}
	for(int i = 1,u,v;i<n;i++)
		cin>>u>>v,g[u].push_back(v),g[v].push_back(u);
	if(n<=10) sub1::main();
	else if(k==1) sub2::main();
	return 0;
}

